package cn.itsource.zcn.org.controller;

import cn.itsource.zcn.org.domain.Department;
import cn.itsource.zcn.org.query.DepartmentQuery;
import cn.itsource.zcn.org.service.IDepartmentService;
import cn.itsource.zcn.utils.AjaxResult;
import cn.itsource.zcn.utils.QueryResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * (Department)表控制层
 *
 * @author makejava
 * @since 2021-08-06 16:14:37
 */
@RestController
@RequestMapping("/dep")
public class DepartmentController {
    /**
     * 服务对象
     */
    @Autowired
    private IDepartmentService departmentService;

    /**
     * 添加或者修改
     * @return AjaxResult
     */
    @PutMapping
    public AjaxResult saveOrUpdate(@RequestBody Department department) {
        try{
            if(department.getId()!=null){
                departmentService.update(department);
            }else{
                departmentService.save(department);
            }
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }
    /**
     * 删除数据
     * @return AjaxResult
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            departmentService.delete(id);
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }

    /**
     * 查询全部数据
     * @return List<>
     */
    @GetMapping
    public List<Department> queryAll() {
        return departmentService.queryAll();
    }

    /**
     * 查询一条数据
     * @return List<>
     */
    @GetMapping("/{id}")
    public Department queryById(@PathVariable("id") Long id) {
        return departmentService.queryById(id);
    }


    /**
     * 查询高级+分页数据
     * @return QueryResult
     */
    @PostMapping
    public QueryResult queryPage(@RequestBody  DepartmentQuery departmentQuery) {
        return departmentService.queryPage(departmentQuery);
    }
    /**
     * 批量删除
     * @return AjaxResult
     */
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody  Long[] ids) {
        try {
            departmentService.patchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }

    }


}
